Skip to content

btreemap: route map .get/.get_mut to member call + e2e parity fixture - #21

Open
shuaimu wants to merge 1 commit into
mainfrom
feature/btreemap-e2e-parity
Open

btreemap: route map .get/.get_mut to member call + e2e parity fixture#21
shuaimu wants to merge 1 commit into
mainfrom
feature/btreemap-e2e-parity

Conversation

@shuaimu

@shuaimu shuaimu commented May 29, 2026

Copy link
Copy Markdown
Owner

The transpiler was lowering m.get(&k) on std::collections::BTreeMap
(and HashMap) through the slice-style rusty::get(container, idx)
helper, which returns an Option<element-by-index> instead of
Option<&V>. The consuming match arm then fails to compile (clang
suppressed-error: "no viable conversion from const rusty::BTreeMap<...>
to function return type int32_t"), and any callsite that destructures
the wrong Option becomes unreachable for the @safe checker.

Changes:

  • transpiler/src/codegen.rs: gate .get/.get_mut slice-style lowering
    on !receiver_is_hashmap_like_expr so map receivers fall through to
    regular method-call emission, which routes to rusty::BTreeMap::get(K) -> Option<V&> directly. Adds 3 codegen unit tests covering BTreeMap
    and HashMap .get/.get_mut.

  • src/main.rs: when analyzing a file marked
    // Auto-generated by rusty-cpp-transpiler, drop the in-module
    rusty::* runtime functions before analysis — those live in the
    rusty/* headers, are noise for the checker on transpiler-generated
    modules, and previously masked any real fixture-side violation behind
    ~250 lines of header-internal findings. Adds two is_* helpers and
    two unit tests.

  • src/parser/ast_visitor.rs: replace SourceRange::tokenize() with a
    thin source-slice lexer (lex_cpp_token_spellings) that returns
    Vec<String> spellings. libclang's tokenize() returns invalid
    token buffers for some generated module ranges and crashes the
    checker on the transpiled .cppm output; the direct-slice path
    side-steps that. Call sites are updated to the new return type.

  • tests/transpile_tests/btreemap: minimal Rust fixture exercising
    BTreeMap::new, insert(K, V) -> Option<V>, and get(&K) -> Option<&V>. Built as a no-workspace [lib] so the parent workspace
    doesn't try to absorb it.

  • tests/transpile_tests/run_btreemap_check.sh (mode 0755): end-to-end
    harness — builds both binaries, transpiles the fixture, injects
    // @safe annotations on the three exported functions, and requires
    a clean rusty-cpp-checker exit on the annotated output.

  • .gitignore + tests/transpile_tests/.gitignore: track the new fixture
    assets and ignore the harness work dir (.rusty-btreemap-check/).

Verified from a clean state of this branch — see the published
scratchpad kind:"diff" record for the rerun results.

Co-Authored-By: Claude Opus 4.7 noreply@anthropic.com

The transpiler was lowering `m.get(&k)` on `std::collections::BTreeMap`
(and `HashMap`) through the slice-style `rusty::get(container, idx)`
helper, which returns an `Option<element-by-index>` instead of
`Option<&V>`. The consuming match arm then fails to compile (clang
suppressed-error: "no viable conversion from `const rusty::BTreeMap<...>`
to function return type `int32_t`"), and any callsite that destructures
the wrong Option becomes unreachable for the @safe checker.

Changes:

* transpiler/src/codegen.rs: gate `.get`/`.get_mut` slice-style lowering
  on `!receiver_is_hashmap_like_expr` so map receivers fall through to
  regular method-call emission, which routes to `rusty::BTreeMap::get(K)
  -> Option<V&>` directly. Adds 3 codegen unit tests covering BTreeMap
  and HashMap `.get`/`.get_mut`.

* src/main.rs: when analyzing a file marked
  `// Auto-generated by rusty-cpp-transpiler`, drop the in-module
  `rusty::*` runtime functions before analysis — those live in the
  rusty/* headers, are noise for the checker on transpiler-generated
  modules, and previously masked any real fixture-side violation behind
  ~250 lines of header-internal findings. Adds two `is_*` helpers and
  two unit tests.

* src/parser/ast_visitor.rs: replace `SourceRange::tokenize()` with a
  thin source-slice lexer (`lex_cpp_token_spellings`) that returns
  `Vec<String>` spellings. libclang's `tokenize()` returns invalid
  token buffers for some generated module ranges and crashes the
  checker on the transpiled `.cppm` output; the direct-slice path
  side-steps that. Call sites are updated to the new return type.

* tests/transpile_tests/btreemap: minimal Rust fixture exercising
  `BTreeMap::new`, `insert(K, V) -> Option<V>`, and `get(&K) ->
  Option<&V>`. Built as a no-workspace `[lib]` so the parent workspace
  doesn't try to absorb it.

* tests/transpile_tests/run_btreemap_check.sh (mode 0755): end-to-end
  harness — builds both binaries, transpiles the fixture, injects
  `// @safe` annotations on the three exported functions, and requires
  a clean `rusty-cpp-checker` exit on the annotated output.

* .gitignore + tests/transpile_tests/.gitignore: track the new fixture
  assets and ignore the harness work dir (`.rusty-btreemap-check/`).

Verified from a clean state of this branch — see the published
scratchpad `kind:"diff"` record for the rerun results.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@shuaimu

shuaimu commented Jun 1, 2026

Copy link
Copy Markdown
Owner Author

This drags in a hand-written include/btree_port/btreemap.hpp, which does not sound right to me?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant